package qc.module.ehs.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.EhsOrganizationTypeEnum;
import qc.module.ehs.dto.org.EhsOrgDto;
import qc.module.ehs.entity.EhsOrg;

import java.util.List;

/**
 * EhsOrgMapper
 *
 * @author QuCheng Tech
 * @create 2024/5/22
 */
@Mapper
public interface EhsOrgMapper {
    EhsOrgMapper MAPPER = Mappers.getMapper(EhsOrgMapper.class);

    @Mapping(target = "orgtype", source = "orgtype.index")
    EhsOrgDto toDto(EhsOrg en);

    @Mapping(target = "orgtype", ignore = true)
    EhsOrg toEntity(EhsOrgDto dto);
    
    List<EhsOrgDto> toDtoList(List<EhsOrg> ens);

    /**
     * 新增修改DTO转换为Entity，特殊处理
     *
     * @param dto 新增DTO
     * @return Entity
     * @author QuCheng Tech
     * @since 2024/5/22
     */
    default EhsOrg dtoToEntity(EhsOrgDto dto) {
        EhsOrg en = EhsOrgMapper.MAPPER.toEntity(dto);

        //处理组织类型
        en.setOrgtype(getEhsOrganizationTypeEnum(dto.getOrgtype()));

        return en;
    }

    /**
     * 根据枚举key获取枚举对象
     *
     * @param index
     * @return
     */
    static EhsOrganizationTypeEnum getEhsOrganizationTypeEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (EhsOrganizationTypeEnum item : EhsOrganizationTypeEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }
        return EhsOrganizationTypeEnum.PRODUCTION;
    }
}
